package com.nine.shop.controller;

import com.nine.shop.model.Admin;
import com.nine.shop.model.User;
import lombok.Data;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * controller公共类提出
 *
 * @author chenyi
 * date: create on 2019/6/8
 */
@Component
@Data
public class BaseController {
    private HttpServletRequest request;
    private HttpServletResponse response;
    private HttpSession session;

    //获取登录信息
    @ModelAttribute
    public void setReqAndResp(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
        this.session = request.getSession();
    }

    /**
     * 获取用户信息
     */
    User getUser() {
        User user = new User();
        return (User) session.getAttribute("userInfo");
    }
    /**
     * 获取管理员信息
     */
     Admin getAdmin() {
        Admin admin = new Admin();
        return (Admin) session.getAttribute("adminInfo");
    }
    /**
     * 添加用户信息
     */
    void setUser(User user) {
        this.session.setAttribute("userInfo", user);
    }

    /**
     * 删除Session用户信息
     */
    void removeUser() {
        this.session.removeAttribute("userInfo");
    }
}
